/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package xpath;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.StringReader;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

/**
 *
 * @author Rishav
 */
public class Xslt {

    static String xsl = "<xsl:stylesheet version=\"1.0\"  xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n"
            + "    <xsl:output omit-xml-declaration=\"yes\" indent=\"yes\"/>\n"
            + "    <xsl:strip-space elements=\"*\"/>\n"
            + "\n"
            + "    <xsl:variable name=\"vApos\">'</xsl:variable>\n"
            + "\n"
            + "    <xsl:template match=\"*[@* or not(*)] \">\n"
            + "      <xsl:if test=\"not(*)\">\n"
            + "         <xsl:apply-templates select=\"ancestor-or-self::*\" mode=\"path\"/>\n"
            + "         <xsl:value-of select=\"concat('=',$vApos,.,$vApos)\"/>\n"
            + "         <xsl:text>&#xA;</xsl:text>\n"
            + "        </xsl:if>\n"
            + "        <xsl:apply-templates select=\"@*|*\"/>\n"
            + "    </xsl:template>\n"
            + "\n"
            + "    <xsl:template match=\"*\" mode=\"path\">\n"
            + "        <xsl:value-of select=\"concat('/',name())\"/>\n"
            + "        <xsl:variable name=\"vnumPrecSiblings\" select=\n"
            + "         \"count(preceding-sibling::*[name()=name(current())])\"/>\n"
            + "        <xsl:if test=\"$vnumPrecSiblings\">\n"
            + "            <xsl:value-of select=\"concat('[', $vnumPrecSiblings +1, ']')\"/>\n"
            + "        </xsl:if>\n"
            + "    </xsl:template>\n"
            + "\n"
            + "    <xsl:template match=\"@*\">\n"
            + "        <xsl:apply-templates select=\"../ancestor-or-self::*\" mode=\"path\"/>\n"
            + "        <xsl:value-of select=\"concat('[@',name(), '=',$vApos,.,$vApos,']')\"/>\n"
            + "        <xsl:text>&#xA;</xsl:text>\n"
            + "    </xsl:template>\n"
            + "</xsl:stylesheet>";

    public static String[] genrateXPath(InputStream xml) throws TransformerConfigurationException, TransformerException {
        StreamSource stylesource = new StreamSource(new StringReader(xsl));
        TransformerFactory tf= TransformerFactory.newInstance();
        Transformer transformer=tf.newTransformer(stylesource);
        Source text = new StreamSource(xml);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(baos);
        transformer.transform(text, new StreamResult(ps));
        return baos.toString().split("\r\n");
    }
}
